<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>GitLab CI/CD 备忘清单
 &#x26;  gitlab-ci cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="本备忘单总结了 GitLab CI/CD 常用的配置说明，以供快速参考

入门，为开发人员分享快速参考备忘单。">
<meta keywords="gitlab-ci,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/gitlab-ci.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.5"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="gitlab-cicd-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 236" height="1em" width="1em">
  <path d="m128.075 236.075 47.104-144.97H80.97l47.104 144.97z" fill="#E24329"></path>
  <path d="M128.075 236.074 80.97 91.104H14.956l113.119 144.97z" fill="#FC6D26"></path>
  <path d="M14.956 91.104.642 135.16a9.752 9.752 0 0 0 3.542 10.903l123.891 90.012-113.12-144.97z" fill="#FCA326"></path>
  <path d="M14.956 91.105H80.97L52.601 3.79c-1.46-4.493-7.816-4.492-9.275 0l-28.37 87.315z" fill="#E24329"></path>
  <path d="m128.075 236.074 47.104-144.97h66.015l-113.12 144.97z" fill="#FC6D26"></path>
  <path d="m241.194 91.104 14.314 44.056a9.752 9.752 0 0 1-3.543 10.903l-123.89 90.012 113.119-144.97z" fill="#FCA326"></path>
  <path d="M241.194 91.105h-66.015l28.37-87.315c1.46-4.493 7.816-4.492 9.275 0l28.37 87.315z" fill="#E24329"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#gitlab-cicd-备忘清单"><span class="icon icon-link"></span></a>GitLab CI/CD 备忘清单</h1><div class="wrap-body">
<p>本备忘单总结了 <a href="https://docs.gitlab.com/ee/ci/yaml/#default">GitLab CI/CD</a> 常用的配置说明，以供快速参考</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#示例">示例</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#关键字">关键字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#关键字-1">关键字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#关键字-2">关键字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#全局关键词">全局关键词</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#全局关键词-1">全局关键词</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#default">default</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#include">include</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#includelocal">include:local</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#includeproject">include:project</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#includeremote">include:remote</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#includetemplate">include:template</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#stages">stages</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#workflow">workflow</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#workflowname">workflow:name</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#workflowrulesvariables">workflow:rules:variables</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#workflowrules">workflow:rules</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#job-关键词">Job 关键词</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#after_script">after_script</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#allow_failure">allow_failure</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#allow_failureexit_codes">allow_failure:exit_codes</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#dast_configuration">dast_configuration</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#before_script">before_script</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#coverage">coverage</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#extends">extends</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#dependencies">dependencies</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#inherit">inherit</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#inheritdefault">inherit:default</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#inheritvariables">inherit:variables</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#interruptible">interruptible</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#pages">pages</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#parallel">parallel</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#parallelmatrix">parallel:matrix</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#resource_group">resource_group</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#tags">tags</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#retry">retry</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#retrywhen">retry:when</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#script">script</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#secrets">secrets</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#secretsvault">secrets:vault</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#secretsfile">secrets:file</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#timeout">timeout</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#services">services</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#servicepull_policy">service:pull_policy</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#stage">stage</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#stage-pre">stage: .pre</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#stage-post">stage: .post</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#trigger">trigger</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#triggerinclude">trigger:include</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#triggerproject">trigger:project</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#triggerstrategy">trigger:strategy</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#triggerforward">trigger:forward</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#variables">variables</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#variablesdescription">variables:description</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#variablesexpand">variables:expand</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#when">when</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#artifacts">artifacts</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactspaths">artifacts:paths</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactsexclude">artifacts:exclude</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactsexpire_in">artifacts:expire_in</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactsexpose_as">artifacts:expose_as</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactsname">artifacts:name</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactspublic">artifacts:public</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactsreports">artifacts:reports</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactsuntracked">artifacts:untracked</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#artifactswhen">artifacts:when</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#cache">cache</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#cachepaths">cache:paths</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#cachekey">cache:key</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#cachekeyfiles">cache:key:files</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#cachekeyprefix">cache:key:prefix</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#cacheuntracked">cache:untracked</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#cachewhen">cache:when</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#cachepolicy">cache:policy</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#environment">environment</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#environmentname">environment:name</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#environmenturl">environment:url</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#environmenton_stop">environment:on_stop</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#environmentaction">environment:action</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#environmentauto_stop_in">environment:auto_stop_in</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#environmentkubernetes">environment:kubernetes</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#environmentdeployment_tier">environment:deployment_tier</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#动态-environment">动态 environment</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#image">image</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#imagename">image:name</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#imageentrypoint">image:entrypoint</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#imagepull_policy">image:pull_policy</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#needs">needs</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#needsartifacts">needs:artifacts</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#needsproject">needs:project</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#needspipelinejob">needs:pipeline:job</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#needsoptional">needs:optional</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#needspipeline">needs:pipeline</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#only--except">only / except</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#onlyrefs--exceptrefs">only:refs / except:refs</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#onlyvariables--exceptvariables">only:variables / except:variables</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#onlychanges--exceptchanges">only:changes / except:changes</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#onlykubernetes--exceptkubernetes">only:kubernetes / except:kubernetes</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#release">release</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#release-参考">release 参考</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#releasetag_name">release:tag_name</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#releasetag_message">release:tag_message</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#releasename">release:name</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#releasedescription">release:description</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#releaseref">release:ref</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#releasemilestones">release:milestones</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#releasereleased_at">release:released_at</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#releaseassetslinks">release:assets:links</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#rules">rules</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#rulesif">rules:if</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#ruleschanges">rules:changes</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#ruleschangespaths">rules:changes:paths</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#ruleschangescompare_to">rules:changes:compare_to</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#rulesexists">rules:exists</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#rulesallow_failure">rules:allow_failure</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#rulesvariables">rules:variables</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="介绍"><a aria-hidden="true" tabindex="-1" href="#介绍"><span class="icon icon-link"></span></a>介绍</h3><div class="wrap-body">
<p>创建并运行您的第一个 GitLab <code>CI/CD</code> 管道，在开始之前，请确保您拥有：</p>
<ul>
<li>GitLab 中您想要使用 CI/CD 的项目</li>
<li>项目的维护者或所有者角色</li>
</ul>
<p>如果您没有项目，可以在 <a href="https://gitlab.com">https://gitlab.com</a> 上免费创建一个公共项目</p>
<ul class="style-timeline">
<li>在存储库的根目录下创建一个 <code>.gitlab-ci.yml</code> 文件。该文件是您定义 <code>CI/CD</code> 作业的地方</li>
<li>转到<code>Settings</code> > <code>CI/CD</code> 并展开运行程序，只要您至少有一个运行器处于活动状态，旁边有一个绿色圆圈，您就有一个运行器可以处理您的工作</li>
</ul>
<!--rehype:className=style-timeline-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="示例"><a aria-hidden="true" tabindex="-1" href="#示例"><span class="icon icon-link"></span></a>示例</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">default</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">image</span><span class="token punctuation">:</span> node<span class="token punctuation">:</span><span class="token number">16</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">windows_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">only</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> master
</span><span class="code-line">  <span class="token key atrule">tags</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> windows
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo Hello<span class="token punctuation">,</span> %USERNAME%<span class="token tag">!</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">linux_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">tags</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> linux
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "Hello<span class="token punctuation">,</span> $USER<span class="token tag">!</span>"
</span></code></pre>
<p><code>tags</code> 用于在不同的平台上运行作业，<code>only</code> 控制 <code>master</code> 分支提交触发</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="关键字"><a aria-hidden="true" tabindex="-1" href="#关键字"><span class="icon icon-link"></span></a>关键字</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="关键字-1"><a aria-hidden="true" tabindex="-1" href="#关键字-1"><span class="icon icon-link"></span></a>关键字</h3><div class="wrap-body">





























<table><thead><tr><th align="left">关键字</th><th>描述</th></tr></thead><tbody><tr><td align="left"><a href="#default">default</a></td><td>工作关键字的自定义默认值 <a href="https://docs.gitlab.com/ee/ci/yaml/#default">#</a></td></tr><tr><td align="left"><a href="#include">include</a></td><td>从其他 YAML 文件导入配置 <a href="https://docs.gitlab.com/ee/ci/yaml/#include">#</a></td></tr><tr><td align="left"><a href="#stages">stages</a></td><td>管道阶段的名称和顺序 <a href="https://docs.gitlab.com/ee/ci/yaml/#stages">#</a></td></tr><tr><td align="left"><a href="#variables">variables</a></td><td>为管道中的所有作业定义 CI/CD 变量 <a href="https://docs.gitlab.com/ee/ci/yaml/#variables">#</a></td></tr><tr><td align="left"><a href="#workflow">workflow</a></td><td>控制运行什么类型的管道 <a href="https://docs.gitlab.com/ee/ci/yaml/#workflow">#</a></td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="关键字-2"><a aria-hidden="true" tabindex="-1" href="#关键字-2"><span class="icon icon-link"></span></a>关键字</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->





































































































































<table><thead><tr><th align="left">关键字</th><th>描述</th></tr></thead><tbody><tr><td align="left"><a href="#after_script">after_script</a></td><td>覆盖一组在作业之后执行的命令 <a href="https://docs.gitlab.com/ee/ci/yaml/#after_script">#</a></td></tr><tr><td align="left"><a href="#allow_failure">allow_failure</a></td><td>允许作业失败。失败的作业不会导致管道失败 <a href="https://docs.gitlab.com/ee/ci/yaml/#allow_failure">#</a></td></tr><tr><td align="left"><a href="#artifacts">artifacts</a></td><td>成功时附加到作业的文件和目录列表 <a href="https://docs.gitlab.com/ee/ci/yaml/#artifacts">#</a></td></tr><tr><td align="left"><a href="#before_script">before_script</a></td><td>覆盖在作业之前执行的一组命令 <a href="https://docs.gitlab.com/ee/ci/yaml/#before_script">#</a></td></tr><tr><td align="left"><a href="#cache">cache</a></td><td>应在后续运行之间缓存的文件列表 <a href="https://docs.gitlab.com/ee/ci/yaml/#cache">#</a></td></tr><tr><td align="left"><a href="#coverage">coverage</a></td><td>给定作业的代码覆盖率设置 <a href="https://docs.gitlab.com/ee/ci/yaml/#coverage">#</a></td></tr><tr><td align="left"><a href="#dast_configuration">dast_configuration</a></td><td>在作业级别使用 DAST 配置文件中的配置 <a href="https://docs.gitlab.com/ee/ci/yaml/#dast_configuration">#</a></td></tr><tr><td align="left"><a href="#dependencies">dependencies</a></td><td>通过提供要从中获取工件的作业列表来限制将哪些工件传递给特定作业 <a href="https://docs.gitlab.com/ee/ci/yaml/#dependencies">#</a></td></tr><tr><td align="left"><a href="#environment">environment</a></td><td>作业部署到的环境的名称 <a href="https://docs.gitlab.com/ee/ci/yaml/#environment">#</a></td></tr><tr><td align="left"><a href="#only--except">except</a></td><td>控制何时不创建作业 <a href="https://docs.gitlab.com/ee/ci/yaml/#only--except">#</a></td></tr><tr><td align="left"><a href="#extends">extends</a></td><td>此作业继承自的配置条目 <a href="https://docs.gitlab.com/ee/ci/yaml/#extends">#</a></td></tr><tr><td align="left"><a href="#image">image</a></td><td>使用 Docker 镜像 <a href="https://docs.gitlab.com/ee/ci/yaml/#image">#</a></td></tr><tr><td align="left"><a href="#inherit">inherit</a></td><td>选择所有作业继承的全局默认值 <a href="https://docs.gitlab.com/ee/ci/yaml/#inherit">#</a></td></tr><tr><td align="left"><a href="#interruptible">interruptible</a></td><td>定义作业是否可以在被较新的运行冗余时取消 <a href="https://docs.gitlab.com/ee/ci/yaml/#interruptible">#</a></td></tr><tr><td align="left"><a href="#needs">needs</a></td><td>在阶段排序之前执行作业 <a href="https://docs.gitlab.com/ee/ci/yaml/#needs">#</a></td></tr><tr><td align="left"><a href="#only--except">only</a></td><td>控制何时创建作业 <a href="https://docs.gitlab.com/ee/ci/yaml/#only--except">#</a></td></tr><tr><td align="left"><a href="#pages">pages</a></td><td>上传作业的结果以与 GitLab Pages 一起使用 <a href="https://docs.gitlab.com/ee/ci/yaml/#pages">#</a></td></tr><tr><td align="left"><a href="#parallel">parallel</a></td><td>应并行运行多少个作业实例 <a href="https://docs.gitlab.com/ee/ci/yaml/#parallel">#</a></td></tr><tr><td align="left"><a href="#release">release</a></td><td>指示运行器生成释放对象 <a href="https://docs.gitlab.com/ee/ci/yaml/#release">#</a></td></tr><tr><td align="left"><a href="#resource_group">resource_group</a></td><td>限制作业并发 <a href="https://docs.gitlab.com/ee/ci/yaml/#resource_group">#</a></td></tr><tr><td align="left"><a href="#retry">retry</a></td><td>发生故障时可以自动重试作业的时间和次数 <a href="https://docs.gitlab.com/ee/ci/yaml/#retry">#</a></td></tr><tr><td align="left"><a href="#rules">rules</a></td><td>用于评估和确定作业的选定属性以及是否创建的条件列表 <a href="https://docs.gitlab.com/ee/ci/yaml/#rules">#</a></td></tr><tr><td align="left"><a href="#script">script</a></td><td>由运行器执行的 Shell 脚本 <a href="https://docs.gitlab.com/ee/ci/yaml/#script">#</a></td></tr><tr><td align="left"><a href="#secrets">secrets</a></td><td>CI/CD 保密工作需要 <a href="https://docs.gitlab.com/ee/ci/yaml/#secrets">#</a></td></tr><tr><td align="left"><a href="#services">services</a></td><td>使用 Docker 服务映像 <a href="https://docs.gitlab.com/ee/ci/yaml/#services">#</a></td></tr><tr><td align="left"><a href="#stage">stage</a></td><td>定义作业阶段 <a href="https://docs.gitlab.com/ee/ci/yaml/#stage">#</a></td></tr><tr><td align="left"><a href="#tags">tags</a></td><td>用于选择跑步者的标签列表 <a href="https://docs.gitlab.com/ee/ci/yaml/#tags">#</a></td></tr><tr><td align="left"><a href="#timeout">timeout</a></td><td>定义优先于项目范围设置的自定义作业级超时 <a href="https://docs.gitlab.com/ee/ci/yaml/#timeout">#</a></td></tr><tr><td align="left"><a href="#trigger">trigger</a></td><td>定义下游管道触发器 <a href="https://docs.gitlab.com/ee/ci/yaml/#trigger">#</a></td></tr><tr><td align="left"><a href="#variables">variables</a></td><td>在作业级别定义作业变量 <a href="https://docs.gitlab.com/ee/ci/yaml/#variables">#</a></td></tr><tr><td align="left"><a href="#when">when</a></td><td>何时运行作业 <a href="https://docs.gitlab.com/ee/ci/yaml/#when">#</a></td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="全局关键词"><a aria-hidden="true" tabindex="-1" href="#全局关键词"><span class="icon icon-link"></span></a>全局关键词</h3><div class="wrap-body">
<ul class="cols-3">
<li><a href="#after_script">after_script</a></li>
<li><a href="#artifacts">artifacts</a></li>
<li><a href="#before_script">before_script</a></li>
<li><a href="#cache">cache</a></li>
<li><a href="#image">image</a></li>
<li><a href="#interruptible">interruptible</a></li>
<li><a href="#retry">retry</a></li>
<li><a href="#services">services</a></li>
<li><a href="#tags">tags</a></li>
<li><a href="#timeout">timeout</a></li>
</ul>
<!--rehype:className=cols-3-->
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="全局关键词-1"><a aria-hidden="true" tabindex="-1" href="#全局关键词-1"><span class="icon icon-link"></span></a>全局关键词</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="default"><a aria-hidden="true" tabindex="-1" href="#default"><span class="icon icon-link"></span></a>default</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">default</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">image</span><span class="token punctuation">:</span> ruby<span class="token punctuation">:</span><span class="token number">3.0</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">rspec</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> bundle exec rspec
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">rspec 2.7</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">image</span><span class="token punctuation">:</span> ruby<span class="token punctuation">:</span><span class="token number">2.7</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> bundle exec rspec
</span></code></pre>
<p>在此示例中，<code>ruby:3.0</code> 是管道中所有作业的默认图像值。<code>rspec 2.7</code> 作业不使用默认值，因为它使用特定于作业的图像部分覆盖了默认值</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="include"><a aria-hidden="true" tabindex="-1" href="#include"><span class="icon icon-link"></span></a>include</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">include</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">local</span><span class="token punctuation">:</span> <span class="token string">'/temp/.gitlab-ci-template.yml'</span>
</span></code></pre>
<p>在 <code>11.4</code> 中移至 <code>GitLab</code> 免费版，使用 <code>include</code> 将外部 <code>YAML</code> 文件包含在您的 <code>CI/CD</code> 配置中</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="includelocal"><a aria-hidden="true" tabindex="-1" href="#includelocal"><span class="icon icon-link"></span></a>include:local</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">include</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">local</span><span class="token punctuation">:</span> <span class="token string">'/temp/.gitlab-ci-template.yml'</span>
</span></code></pre>
<p>使用 <code>include:local</code> 包含与 <code>.gitlab-ci.yml</code> 文件位于同一存储库中的文件</p>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="includeproject"><a aria-hidden="true" tabindex="-1" href="#includeproject"><span class="icon icon-link"></span></a>include:project</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<p>要在同一个 GitLab 实例上包含来自另一个私有项目的文件，请使用 <code>include:project</code> 和 <code>include:file</code></p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">include</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">project</span><span class="token punctuation">:</span> <span class="token string">'group/my-project'</span>
</span><span class="code-line">    <span class="token key atrule">file</span><span class="token punctuation">:</span> <span class="token string">'/temp/.gitlab-ci-template.yml'</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">project</span><span class="token punctuation">:</span> <span class="token string">'group/subgroup/my-project-2'</span>
</span><span class="code-line">    <span class="token key atrule">file</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> <span class="token string">'/temp/.builds.yml'</span>
</span><span class="code-line">      <span class="token punctuation">-</span> <span class="token string">'/temp/.tests.yml'</span>
</span></code></pre>
<p>您还可以指定一个 <code>ref</code>：</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">include</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">project</span><span class="token punctuation">:</span> <span class="token string">'group/my-project'</span>
</span><span class="code-line">    <span class="token key atrule">ref</span><span class="token punctuation">:</span> main    <span class="token comment"># Git branch</span>
</span><span class="code-line">    <span class="token key atrule">file</span><span class="token punctuation">:</span> <span class="token string">'/templates/.gitlab-ci.yml'</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">project</span><span class="token punctuation">:</span> <span class="token string">'group/my-project'</span>
</span><span class="code-line">    <span class="token key atrule">ref</span><span class="token punctuation">:</span> v1.0.0   <span class="token comment"># Git Tag</span>
</span><span class="code-line">    <span class="token key atrule">file</span><span class="token punctuation">:</span> <span class="token string">'/templates/.gitlab-ci.yml'</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">project</span><span class="token punctuation">:</span> <span class="token string">'group/my-project'</span>
</span><span class="code-line">    <span class="token key atrule">ref</span><span class="token punctuation">:</span> 787123b  <span class="token comment"># Git SHA</span>
</span><span class="code-line">    <span class="token key atrule">file</span><span class="token punctuation">:</span> <span class="token string">'/templates/.gitlab-ci.yml'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="includeremote"><a aria-hidden="true" tabindex="-1" href="#includeremote"><span class="icon icon-link"></span></a>include:remote</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">include</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">remote</span><span class="token punctuation">:</span> <span class="token string">'https://gitlab.com/example-project/-/raw/main/.gitlab-ci.yml'</span>
</span></code></pre>
<p>使用带有完整 <code>URL</code> 的 <code>include:remote</code> 来包含来自不同位置的文件</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="includetemplate"><a aria-hidden="true" tabindex="-1" href="#includetemplate"><span class="icon icon-link"></span></a>include:template</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>使用 <code>include:template</code> 来包含 <code>.gitlab-ci.yml</code> 模板</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token comment"># 文件来自 GitLab 模板集合</span>
</span><span class="code-line"><span class="token key atrule">include</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">template</span><span class="token punctuation">:</span> Auto<span class="token punctuation">-</span>DevOps.gitlab<span class="token punctuation">-</span>ci.yml
</span></code></pre>
<p>多个 <code>include:template</code> 文件：</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">include</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">template</span><span class="token punctuation">:</span> Android<span class="token punctuation">-</span>Fastlane.gitlab<span class="token punctuation">-</span>ci.yml
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">template</span><span class="token punctuation">:</span> Auto<span class="token punctuation">-</span>DevOps.gitlab<span class="token punctuation">-</span>ci.yml
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="stages"><a aria-hidden="true" tabindex="-1" href="#stages"><span class="icon icon-link"></span></a>stages</h3><div class="wrap-body">
<p>使用阶段来定义包含作业组的阶段。如果 <code>.gitlab-ci.yml</code> 文件中未定义阶段，则默认管道阶段为：</p>
<ul class="cols-2">
<li><a href="#stage-pre">.pre</a></li>
<li>build</li>
<li>test</li>
<li>deploy</li>
<li><a href="#stage-post">.post</a></li>
</ul>
<!--rehype:className=cols-2-->
<hr>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">stages</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> build
</span><span class="code-line">  <span class="token punctuation">-</span> test
</span><span class="code-line">  <span class="token punctuation">-</span> deploy
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="workflow"><a aria-hidden="true" tabindex="-1" href="#workflow"><span class="icon icon-link"></span></a>workflow</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="workflowname"><a aria-hidden="true" tabindex="-1" href="#workflowname"><span class="icon icon-link"></span></a>workflow:name</h3><div class="wrap-body">
<p>您可以在 workflow: 中使用 name 来定义管道的名称</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">workflow</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">name</span><span class="token punctuation">:</span> <span class="token string">'分支管道：$CI_COMMIT_BRANCH'</span>
</span></code></pre>
<p>根据管道条件具有不同管道名称的配置：</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">PIPELINE_NAME</span><span class="token punctuation">:</span> <span class="token string">'默认管道名称'</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">workflow</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">name</span><span class="token punctuation">:</span> <span class="token string">'$PIPELINE_NAME'</span>
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> <span class="token string">'$CI_PIPELINE_SOURCE == "merge_request_event"'</span>
</span><span class="code-line">      <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token key atrule">PIPELINE_NAME</span><span class="token punctuation">:</span> <span class="token string">'MR pipeline: $CI_COMMIT_BRANCH'</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> <span class="token string">'$CI_MERGE_REQUEST_LABELS =~ /pipeline:run-in-ruby3/'</span>
</span><span class="code-line">      <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token key atrule">PIPELINE_NAME</span><span class="token punctuation">:</span> <span class="token string">'Ruby 3 pipeline'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="workflowrulesvariables"><a aria-hidden="true" tabindex="-1" href="#workflowrulesvariables"><span class="icon icon-link"></span></a>workflow:rules:variables</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->
<p>您可以在 <code>workflow:rules</code> 中使用变量来定义特定管道条件的变量</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">DEPLOY_VARIABLE</span><span class="token punctuation">:</span> <span class="token string">"default-deploy"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">workflow</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
</span><span class="code-line">      <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token key atrule">DEPLOY_VARIABLE</span><span class="token punctuation">:</span> <span class="token string">"deploy-production"</span>  <span class="token comment"># 覆盖全局定义的 DEPLOY_VARIABLE</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_REF_NAME =~ /feature/
</span><span class="code-line">      <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token key atrule">IS_A_FEATURE</span><span class="token punctuation">:</span> <span class="token string">"true"</span>                  <span class="token comment"># 定义一个新变量</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">when</span><span class="token punctuation">:</span> always                            <span class="token comment"># 在其他情况下运行管道</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">DEPLOY_VARIABLE</span><span class="token punctuation">:</span> <span class="token string">"job1-default-deploy"</span>
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
</span><span class="code-line">      <span class="token key atrule">variables</span><span class="token punctuation">:</span>                                   <span class="token comment"># 覆盖定义的 DEPLOY_VARIABLE</span>
</span><span class="code-line">        <span class="token key atrule">DEPLOY_VARIABLE</span><span class="token punctuation">:</span> <span class="token string">"job1-deploy-production"</span>  <span class="token comment"># 在工作层面。</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">when</span><span class="token punctuation">:</span> on_success                             <span class="token comment"># 在其他情况下运行作业</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "以 $DEPLOY_VARIABLE 作为参数运行脚本"
</span><span class="code-line">    <span class="token punctuation">-</span> echo "如果 $IS_A_FEATURE 存在则运行另一个脚本"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "以 $DEPLOY_VARIABLE 作为参数运行脚本"
</span><span class="code-line">    <span class="token punctuation">-</span> echo "如果 $IS_A_FEATURE 存在则运行另一个脚本"
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="workflowrules"><a aria-hidden="true" tabindex="-1" href="#workflowrules"><span class="icon icon-link"></span></a>workflow:rules</h3><div class="wrap-body">
<p>工作流(workflow)中的 <code>rules</code> 关键字类似于作业中定义的 <a href="#rules"><code>rules</code></a>，但控制是否创建整个管道</p>
<ul class="cols-2">
<li><a href="#rulesif">rules: if</a></li>
<li><a href="#ruleschanges">rules: changes</a></li>
<li><a href="#rulesexists">rules: exists</a></li>
<li><a href="#when">when</a></li>
<li><a href="#workflowrulesvariables">variables</a></li>
</ul>
<!--rehype:className=cols-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">workflow</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_TITLE =~ /<span class="token punctuation">-</span>draft$/
</span><span class="code-line">      <span class="token key atrule">when</span><span class="token punctuation">:</span> never
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_PIPELINE_SOURCE == "merge_request_event"
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="job-关键词"><a aria-hidden="true" tabindex="-1" href="#job-关键词"><span class="icon icon-link"></span></a>Job 关键词</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="after_script"><a aria-hidden="true" tabindex="-1" href="#after_script"><span class="icon icon-link"></span></a>after_script</h3><div class="wrap-body">
<p>在每个作业（包括失败的作业）<strong>之后</strong>运行的命令数组</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "示例脚本部分"
</span><span class="code-line">  <span class="token key atrule">after_script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "在“script”部分完成后执行此命令"
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-4"><div class="wrap-header h3wrap"><h3 id="allow_failure"><a aria-hidden="true" tabindex="-1" href="#allow_failure"><span class="icon icon-link"></span></a>allow_failure</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4-->
<p>确定管道是否应在作业失败时继续运行</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> execute_script_1
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> execute_script_2
</span><span class="code-line">  <span class="token key atrule">allow_failure</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job3</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> deploy_to_staging
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> staging
</span></code></pre>
<h4 id="allow_failureexit_codes"><a aria-hidden="true" tabindex="-1" href="#allow_failureexit_codes"><span class="icon icon-link"></span></a>allow_failure:exit_codes</h4>
<p>控制何时允许作业失败。对于任何列出的退出代码，作业是 <code>allow_failure: true</code>，对于任何其他退出代码，<code>allow_failure</code> false</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">test_job_1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "退出代码 1 的脚本。此作业失败"
</span><span class="code-line">    <span class="token punctuation">-</span> exit 1
</span><span class="code-line">  <span class="token key atrule">allow_failure</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">exit_codes</span><span class="token punctuation">:</span> <span class="token number">137</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test_job_2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "退出代码 137 的脚本允许此作业失败"
</span><span class="code-line">    <span class="token punctuation">-</span> exit 137
</span><span class="code-line">  <span class="token key atrule">allow_failure</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">exit_codes</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> <span class="token number">137</span>
</span><span class="code-line">      <span class="token punctuation">-</span> <span class="token number">255</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="dast_configuration"><a aria-hidden="true" tabindex="-1" href="#dast_configuration"><span class="icon icon-link"></span></a>dast_configuration</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">stages</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> build
</span><span class="code-line">  <span class="token punctuation">-</span> dast
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">include</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> <span class="token key atrule">template</span><span class="token punctuation">:</span> DAST.gitlab<span class="token punctuation">-</span>ci.yml
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">dast</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">dast_configuration</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">site_profile</span><span class="token punctuation">:</span> <span class="token string">"Example Co"</span>
</span><span class="code-line">    <span class="token key atrule">scanner_profile</span><span class="token punctuation">:</span> <span class="token string">"Quick Passive Test"</span>
</span></code></pre>
<p>指定要在 CI/CD 配置中使用的站点配置文件和扫描仪配置文件。 两个配置文件必须首先在项目中创建。 作业的阶段必须快</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="before_script"><a aria-hidden="true" tabindex="-1" href="#before_script"><span class="icon icon-link"></span></a>before_script</h3><div class="wrap-body">
<p>在每个作业的 <code>script</code> 命令之前运行，但在工件恢复之后运行</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">before_script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "在任何“script<span class="token punctuation">:</span>”命令之前执行此命令"
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "“before_script”命令之后执行"
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="coverage"><a aria-hidden="true" tabindex="-1" href="#coverage"><span class="icon icon-link"></span></a>coverage</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> rspec
</span><span class="code-line">  <span class="token key atrule">coverage</span><span class="token punctuation">:</span> <span class="token string">'/Code coverage: \d+\.\d+/'</span>
</span></code></pre>
<p>使用自定义正则表达式的覆盖率来配置如何从作业输出中提取代码覆盖率</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="extends"><a aria-hidden="true" tabindex="-1" href="#extends"><span class="icon icon-link"></span></a>extends</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">.tests</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> rake test
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">only</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">refs</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> branches
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">rspec</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">extends</span><span class="token punctuation">:</span> .tests
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> rake rspec
</span><span class="code-line">  <span class="token key atrule">only</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> $RSPEC
</span></code></pre>
<p>使用 <code>extends</code> 重用配置部分。 它是 YAML 锚点的替代品，并且更加灵活和可读</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="dependencies"><a aria-hidden="true" tabindex="-1" href="#dependencies"><span class="icon icon-link"></span></a>dependencies</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">build osx</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> make build<span class="token punctuation">:</span>osx
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> binaries/
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">build linux</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> make build<span class="token punctuation">:</span>linux
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> binaries/
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test osx</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> make test<span class="token punctuation">:</span>osx
</span><span class="code-line">  <span class="token key atrule">dependencies</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> build osx
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test linux</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> make test<span class="token punctuation">:</span>linux
</span><span class="code-line">  <span class="token key atrule">dependencies</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> build linux
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">deploy</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> make deploy
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> production
</span></code></pre>
<p>定义要从中获取工件的作业列表。您还可以将作业设置为根本不下载任何工件</p>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="inherit"><a aria-hidden="true" tabindex="-1" href="#inherit"><span class="icon icon-link"></span></a>inherit</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>使用 inherit 控制默认关键字和变量的继承</p>
<h4 id="inheritdefault"><a aria-hidden="true" tabindex="-1" href="#inheritdefault"><span class="icon icon-link"></span></a>inherit:default</h4>
<p>使用 <code>inherit:default</code> 控制 <code>default</code> 关键字的继承</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">default</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">retry</span><span class="token punctuation">:</span> <span class="token number">2</span>
</span><span class="code-line">  <span class="token key atrule">image</span><span class="token punctuation">:</span> ruby<span class="token punctuation">:</span><span class="token number">3.0</span>
</span><span class="code-line">  <span class="token key atrule">interruptible</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "此作业不继承任何默认关键字"
</span><span class="code-line">  <span class="token key atrule">inherit</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">default</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "此作业仅继承列出的两个默认关键字。它不继承“可中断”"
</span><span class="code-line">  <span class="token key atrule">inherit</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">default</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> retry
</span><span class="code-line">      <span class="token punctuation">-</span> image
</span></code></pre>
<h4 id="inheritvariables"><a aria-hidden="true" tabindex="-1" href="#inheritvariables"><span class="icon icon-link"></span></a>inherit:variables</h4>
<p>使用 inherit:variables 控制全局变量关键字的继承</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">VARIABLE1</span><span class="token punctuation">:</span> <span class="token string">"这是变量 1"</span>
</span><span class="code-line">  <span class="token key atrule">VARIABLE2</span><span class="token punctuation">:</span> <span class="token string">"这是变量 2"</span>
</span><span class="code-line">  <span class="token key atrule">VARIABLE3</span><span class="token punctuation">:</span> <span class="token string">"这是变量 3"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "该作业不继承任何全局变量"
</span><span class="code-line">  <span class="token key atrule">inherit</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">variables</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "此作业仅继承列出的两个全局变量。它不继承“VARIABLE3”"
</span><span class="code-line">  <span class="token key atrule">inherit</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> VARIABLE1
</span><span class="code-line">      <span class="token punctuation">-</span> VARIABLE2
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="interruptible"><a aria-hidden="true" tabindex="-1" href="#interruptible"><span class="icon icon-link"></span></a>interruptible</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">stages</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> stage1
</span><span class="code-line">  <span class="token punctuation">-</span> stage2
</span><span class="code-line">  <span class="token punctuation">-</span> stage3
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">step-1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> stage1
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "可以取消"
</span><span class="code-line">  <span class="token key atrule">interruptible</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">step-2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> stage2
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "不能取消"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">step-3</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> stage3
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "因为第 2 步无法取消，所以此步骤永远无法取消，即使它被设置为可中断"
</span><span class="code-line">  <span class="token key atrule">interruptible</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span></code></pre>
<p>如果在作业完成之前启动较新的管道时应取消作业，则使用可中断的</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="pages"><a aria-hidden="true" tabindex="-1" href="#pages"><span class="icon icon-link"></span></a>pages</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">pages</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> mkdir .public
</span><span class="code-line">    <span class="token punctuation">-</span> cp <span class="token punctuation">-</span>r * .public
</span><span class="code-line">    <span class="token punctuation">-</span> mv .public public
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> public
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> production
</span></code></pre>
<p>使用页面定义将静态内容上传到 <code>GitLab</code> 的 <code>GitLab Pages</code> 作业。然后将内容发布为网站</p>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="parallel"><a aria-hidden="true" tabindex="-1" href="#parallel"><span class="icon icon-link"></span></a>parallel</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">test</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> rspec
</span><span class="code-line">  <span class="token key atrule">parallel</span><span class="token punctuation">:</span> <span class="token number">5</span>
</span></code></pre>
<p>使用 <code>parallel</code> 在单个管道中并行多次运行作业</p>
<h4 id="parallelmatrix"><a aria-hidden="true" tabindex="-1" href="#parallelmatrix"><span class="icon icon-link"></span></a>parallel:matrix</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploystacks</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> bin/deploy
</span><span class="code-line">  <span class="token key atrule">parallel</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">matrix</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> <span class="token key atrule">PROVIDER</span><span class="token punctuation">:</span> aws
</span><span class="code-line">        <span class="token key atrule">STACK</span><span class="token punctuation">:</span>
</span><span class="code-line">          <span class="token punctuation">-</span> monitoring
</span><span class="code-line">          <span class="token punctuation">-</span> app1
</span><span class="code-line">          <span class="token punctuation">-</span> app2
</span><span class="code-line">      <span class="token punctuation">-</span> <span class="token key atrule">PROVIDER</span><span class="token punctuation">:</span> ovh
</span><span class="code-line">        <span class="token key atrule">STACK</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>monitoring<span class="token punctuation">,</span> backup<span class="token punctuation">,</span> app<span class="token punctuation">]</span>
</span><span class="code-line">      <span class="token punctuation">-</span> <span class="token key atrule">PROVIDER</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>gcp<span class="token punctuation">,</span> vultr<span class="token punctuation">]</span>
</span><span class="code-line">        <span class="token key atrule">STACK</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>data<span class="token punctuation">,</span> processing<span class="token punctuation">]</span>
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> $PROVIDER/$STACK
</span></code></pre>
<p>使用 <code>parallel:matrix</code> 在单个管道中并行多次运行作业，但每个作业实例具有不同的变量值</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="resource_group"><a aria-hidden="true" tabindex="-1" href="#resource_group"><span class="icon icon-link"></span></a>resource_group</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploy-to-production</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">resource_group</span><span class="token punctuation">:</span> production
</span></code></pre>
<p>创建一个资源组，以确保作业在同一项目的不同管道之间互斥</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="tags"><a aria-hidden="true" tabindex="-1" href="#tags"><span class="icon icon-link"></span></a>tags</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">tags</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> ruby
</span><span class="code-line">    <span class="token punctuation">-</span> postgres
</span></code></pre>
<p>使用标签从项目可用的所有运行器列表中选择特定运行器</p>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="retry"><a aria-hidden="true" tabindex="-1" href="#retry"><span class="icon icon-link"></span></a>retry</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">test</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> rspec
</span><span class="code-line">  <span class="token key atrule">retry</span><span class="token punctuation">:</span> <span class="token number">2</span>
</span></code></pre>
<p>使用 <code>retry</code> 配置作业失败时重试的次数。如果未定义，则默认为 <code>0</code>，并且作业不会重试</p>
<h4 id="retrywhen"><a aria-hidden="true" tabindex="-1" href="#retrywhen"><span class="icon icon-link"></span></a>retry:when</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">test</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> rspec
</span><span class="code-line">  <span class="token key atrule">retry</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">max</span><span class="token punctuation">:</span> <span class="token number">2</span>
</span><span class="code-line">    <span class="token key atrule">when</span><span class="token punctuation">:</span> runner_system_failure
</span></code></pre>
<p>使用 <code>retry:when</code> 和 <code>retry:max</code> 来重试特定失败案例的作业。 <code>retry:max</code> 是最大重试次数，和<code>retry</code>一样，可以是0、1、2</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="script"><a aria-hidden="true" tabindex="-1" href="#script"><span class="icon icon-link"></span></a>script</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> <span class="token string">"bundle exec rspec"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> uname <span class="token punctuation">-</span>a
</span><span class="code-line">    <span class="token punctuation">-</span> bundle exec rspec
</span></code></pre>
<p>使用脚本指定运行器执行的命令</p>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="secrets"><a aria-hidden="true" tabindex="-1" href="#secrets"><span class="icon icon-link"></span></a>secrets</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>此关键字必须与 <code>secrets:vault</code> 一起使用</p>
<h4 id="secretsvault"><a aria-hidden="true" tabindex="-1" href="#secretsvault"><span class="icon icon-link"></span></a>secrets:vault</h4>
<p>使用 <code>secrets:vault</code> 指定 HashiCorp Vault 提供的秘密</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">secrets</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token comment"># 将秘密的路径存储在此 CI/CD 变量中</span>
</span><span class="code-line">    <span class="token key atrule">DATABASE_PASSWORD</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token comment"># 转换为秘密：`ops/data/production/db`</span>
</span><span class="code-line">      <span class="token comment"># 字段：`password`</span>
</span><span class="code-line">      <span class="token key atrule">vault</span><span class="token punctuation">:</span> 
</span><span class="code-line">        <span class="token key atrule">engine</span><span class="token punctuation">:</span>
</span><span class="code-line">          <span class="token key atrule">name</span><span class="token punctuation">:</span> kv<span class="token punctuation">-</span>v2
</span><span class="code-line">          <span class="token key atrule">path</span><span class="token punctuation">:</span> ops
</span><span class="code-line">        <span class="token key atrule">path</span><span class="token punctuation">:</span> production/db
</span><span class="code-line">        <span class="token key atrule">field</span><span class="token punctuation">:</span> password
</span></code></pre>
<h4 id="secretsfile"><a aria-hidden="true" tabindex="-1" href="#secretsfile"><span class="icon icon-link"></span></a>secrets:file</h4>
<p>使用 <code>secrets:file</code> 将秘密配置为存储为文件或变量类型的 CI/CD 变量</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">secrets</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">DATABASE_PASSWORD</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token key atrule">vault</span><span class="token punctuation">:</span> production/db/password@ops
</span><span class="code-line">      <span class="token key atrule">file</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="timeout"><a aria-hidden="true" tabindex="-1" href="#timeout"><span class="icon icon-link"></span></a>timeout</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">build</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> build.sh
</span><span class="code-line">  <span class="token key atrule">timeout</span><span class="token punctuation">:</span> 3 hours 30 minutes
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> rspec
</span><span class="code-line">  <span class="token key atrule">timeout</span><span class="token punctuation">:</span> 3h 30m
</span></code></pre>
<p>为特定作业配置超时。 如果作业运行时间超过超时时间，则作业失败</p>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="services"><a aria-hidden="true" tabindex="-1" href="#services"><span class="icon icon-link"></span></a>services</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>使用服务指定额外的 <code>Docker</code> 映像以在其中运行脚本</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">default</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">image</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> ruby<span class="token punctuation">:</span><span class="token number">2.6</span>
</span><span class="code-line">    <span class="token key atrule">entrypoint</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"/bin/bash"</span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token key atrule">services</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>postgres<span class="token punctuation">:</span><span class="token number">11.7</span>
</span><span class="code-line">      <span class="token key atrule">alias</span><span class="token punctuation">:</span> db<span class="token punctuation">-</span>postgres
</span><span class="code-line">      <span class="token key atrule">entrypoint</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"/usr/local/bin/db-postgres"</span><span class="token punctuation">]</span>
</span><span class="code-line">      <span class="token key atrule">command</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"start"</span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token key atrule">before_script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> bundle install
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> bundle exec rake spec
</span></code></pre>
<h4 id="servicepull_policy"><a aria-hidden="true" tabindex="-1" href="#servicepull_policy"><span class="icon icon-link"></span></a>service:pull_policy</h4>
<p>运行器用于获取 <code>Docker</code> 映像的拉取策略</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "A single pull policy."
</span><span class="code-line">  <span class="token key atrule">services</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> postgres<span class="token punctuation">:</span><span class="token number">11.6</span>
</span><span class="code-line">      <span class="token key atrule">pull_policy</span><span class="token punctuation">:</span> if<span class="token punctuation">-</span>not<span class="token punctuation">-</span>present
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "Multiple pull policies."
</span><span class="code-line">  <span class="token key atrule">services</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> postgres<span class="token punctuation">:</span><span class="token number">11.6</span>
</span><span class="code-line">      <span class="token key atrule">pull_policy</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>always<span class="token punctuation">,</span> if<span class="token punctuation">-</span>not<span class="token punctuation">-</span>present<span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="stage"><a aria-hidden="true" tabindex="-1" href="#stage"><span class="icon icon-link"></span></a>stage</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">stages</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> build
</span><span class="code-line">  <span class="token punctuation">-</span> test
</span><span class="code-line">  <span class="token punctuation">-</span> deploy
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "这项工作编译代码"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "此作业测试编译后的代码。它在构建阶段完成时运行"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job3</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "这项工作也在测试阶段运行"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job4</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "此作业部署代码。它在测试阶段完成时运行"
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> production
</span></code></pre>
<h4 id="stage-pre"><a aria-hidden="true" tabindex="-1" href="#stage-pre"><span class="icon icon-link"></span></a>stage: .pre</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">stages</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> build
</span><span class="code-line">  <span class="token punctuation">-</span> test
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "该作业在构建阶段运行"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">first-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> .pre
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "该作业在 .pre 阶段运行，在所有其他阶段之前"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "该作业在测试阶段运行"
</span></code></pre>
<h4 id="stage-post"><a aria-hidden="true" tabindex="-1" href="#stage-post"><span class="icon icon-link"></span></a>stage: .post</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">stages</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> build
</span><span class="code-line">  <span class="token punctuation">-</span> test
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "该作业在构建阶段运行"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">last-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> .post
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "该作业在 .post 阶段运行，在所有其他阶段之后"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "该作业在测试阶段运行"
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="trigger"><a aria-hidden="true" tabindex="-1" href="#trigger"><span class="icon icon-link"></span></a>trigger</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>声明一个作业是一个“触发作业”</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">trigger-multi-project-pipeline</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">trigger</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>group/my<span class="token punctuation">-</span>project
</span></code></pre>
<h4 id="triggerinclude"><a aria-hidden="true" tabindex="-1" href="#triggerinclude"><span class="icon icon-link"></span></a>trigger:include</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">trigger-child-pipeline</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">trigger</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">include</span><span class="token punctuation">:</span> path/to/child<span class="token punctuation">-</span>pipeline.gitlab<span class="token punctuation">-</span>ci.yml
</span></code></pre>
<p>声明作业是启动子管道的“触发器作业”</p>
<h4 id="triggerproject"><a aria-hidden="true" tabindex="-1" href="#triggerproject"><span class="icon icon-link"></span></a>trigger:project</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">trigger-multi-project-pipeline</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">trigger</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">project</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>group/my<span class="token punctuation">-</span>project
</span></code></pre>
<p>声明作业是启动多项目管道的“触发器作业”</p>
<h4 id="triggerstrategy"><a aria-hidden="true" tabindex="-1" href="#triggerstrategy"><span class="icon icon-link"></span></a>trigger:strategy</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">trigger-multi-project-pipeline</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">trigger</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">project</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>group/my<span class="token punctuation">-</span>project
</span><span class="code-line">    <span class="token key atrule">branch</span><span class="token punctuation">:</span> development
</span></code></pre>
<p>强制触发作业等待下游管道完成后再标记为成功</p>
<h4 id="triggerforward"><a aria-hidden="true" tabindex="-1" href="#triggerforward"><span class="icon icon-link"></span></a>trigger:forward</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">variables</span><span class="token punctuation">:</span> <span class="token comment"># 每个作业的默认变量</span>
</span><span class="code-line">  <span class="token key atrule">VAR</span><span class="token punctuation">:</span> value
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 默认行为：</span>
</span><span class="code-line"><span class="token comment"># - VAR 传递给孩子</span>
</span><span class="code-line"><span class="token comment"># - MYVAR 没有传递给孩子</span>
</span><span class="code-line"><span class="token key atrule">child1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">trigger</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">include</span><span class="token punctuation">:</span> .child<span class="token punctuation">-</span>pipeline.yml
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 转发管道变量：</span>
</span><span class="code-line"><span class="token comment"># - VAR 传递给孩子</span>
</span><span class="code-line"><span class="token comment"># - MYVAR 传递给孩子</span>
</span><span class="code-line"><span class="token key atrule">child2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">trigger</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">include</span><span class="token punctuation">:</span> .child<span class="token punctuation">-</span>pipeline.yml
</span><span class="code-line">    <span class="token key atrule">forward</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token key atrule">pipeline_variables</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 不要转发 YAML 变量：</span>
</span><span class="code-line"><span class="token comment"># - VAR 不会传递给孩子</span>
</span><span class="code-line"><span class="token comment"># - MYVAR 没有传递给孩子</span>
</span><span class="code-line"><span class="token key atrule">child3</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">trigger</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">include</span><span class="token punctuation">:</span> .child<span class="token punctuation">-</span>pipeline.yml
</span><span class="code-line">    <span class="token key atrule">forward</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token key atrule">yaml_variables</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="variables"><a aria-hidden="true" tabindex="-1" href="#variables"><span class="icon icon-link"></span></a>variables</h3><div class="wrap-body">
<p>CI/CD 变量是传递给作业的可配置值。使用变量创建自定义变量</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">DEPLOY_SITE</span><span class="token punctuation">:</span> <span class="token string">"https://example.com/"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">deploy_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> deploy<span class="token punctuation">-</span>script <span class="token punctuation">-</span><span class="token punctuation">-</span>url $DEPLOY_SITE <span class="token punctuation">-</span><span class="token punctuation">-</span>path "/"
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> production
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">deploy_review_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">REVIEW_PATH</span><span class="token punctuation">:</span> <span class="token string">"/review"</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> deploy<span class="token punctuation">-</span>review<span class="token punctuation">-</span>script <span class="token punctuation">-</span><span class="token punctuation">-</span>url $DEPLOY_SITE <span class="token punctuation">-</span><span class="token punctuation">-</span>path $REVIEW_PATH
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> production
</span></code></pre>
<h4 id="variablesdescription"><a aria-hidden="true" tabindex="-1" href="#variablesdescription"><span class="icon icon-link"></span></a>variables:description</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">DEPLOY_ENVIRONMENT</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">description</span><span class="token punctuation">:</span> <span class="token string">"部署目标。 如果需要，将此变量更改为“canary”或“production”"</span>
</span><span class="code-line">    <span class="token key atrule">value</span><span class="token punctuation">:</span> <span class="token string">"staging"</span>
</span></code></pre>
<h4 id="variablesexpand"><a aria-hidden="true" tabindex="-1" href="#variablesexpand"><span class="icon icon-link"></span></a>variables:expand</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">VAR1</span><span class="token punctuation">:</span> value1
</span><span class="code-line">  <span class="token key atrule">VAR2</span><span class="token punctuation">:</span> value2 $VAR1
</span><span class="code-line">  <span class="token key atrule">VAR3</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">value</span><span class="token punctuation">:</span> value3 $VAR1
</span><span class="code-line">    <span class="token key atrule">expand</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
</span></code></pre>
<p>使用 <code>expand</code> 关键字将变量配置为可扩展或不可扩展</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="when"><a aria-hidden="true" tabindex="-1" href="#when"><span class="icon icon-link"></span></a>when</h3><div class="wrap-body">
<p>使用 <code>when</code> 配置作业运行的条件。如果未在作业中定义，则默认值为 <code>when:on_success</code></p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">stages</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token punctuation">-</span> build
</span><span class="code-line">  <span class="token punctuation">-</span> cleanup_build
</span><span class="code-line">  <span class="token punctuation">-</span> test
</span><span class="code-line">  <span class="token punctuation">-</span> deploy
</span><span class="code-line">  <span class="token punctuation">-</span> cleanup
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">build_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> make build
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">cleanup_build_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> cleanup_build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> cleanup build when failed
</span><span class="code-line">  <span class="token key atrule">when</span><span class="token punctuation">:</span> on_failure
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> make test
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">deploy_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> make deploy
</span><span class="code-line">  <span class="token key atrule">when</span><span class="token punctuation">:</span> manual
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> production
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">cleanup_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> cleanup
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> cleanup after jobs
</span><span class="code-line">  <span class="token key atrule">when</span><span class="token punctuation">:</span> always
</span></code></pre>
<hr>

































<table class="style-list"><thead><tr><th align="left">:-</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>on_success(default)</code></td><td>仅当早期阶段的所有作业都成功或具有 <code>allow_failure: true</code> 时才运行该作业</td></tr><tr><td align="left"><code>manual</code></td><td>仅在手动触发时运行作业</td></tr><tr><td align="left"><code>always</code></td><td>无论早期阶段的作业状态如何，都运行作业。也可以用在 <code>workflow:rules</code> 中</td></tr><tr><td align="left"><code>on_failure</code></td><td>仅当早期阶段的至少一项作业失败时才运行该作业</td></tr><tr><td align="left"><code>delayed</code></td><td>将作业的执行延迟指定的持续时间</td></tr><tr><td align="left"><code>never</code></td><td>不要运行作业。只能在规则<a href="#rules">rules</a>部分或工作流中使用：<code>workflow: rules</code></td></tr></tbody></table>
<!--rehype:className=style-list-->
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="artifacts"><a aria-hidden="true" tabindex="-1" href="#artifacts"><span class="icon icon-link"></span></a>artifacts</h2><div class="wrap-body">
<p>使用工件指定要将哪些文件另存为作业 artifacts。作业 artifacts 是作业成功、失败或始终附加到作业的文件和目录的列表</p>
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactspaths"><a aria-hidden="true" tabindex="-1" href="#artifactspaths"><span class="icon icon-link"></span></a>artifacts:paths</h3><div class="wrap-body">
<p>路径是相对于项目目录（$CI_PROJECT_DIR）的，不能直接链接到项目目录之外</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> binaries/
</span><span class="code-line">      <span class="token punctuation">-</span> .config
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactsexclude"><a aria-hidden="true" tabindex="-1" href="#artifactsexclude"><span class="icon icon-link"></span></a>artifacts:exclude</h3><div class="wrap-body">
<p>防止将文件添加到 artifacts 存档中</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> binaries/
</span><span class="code-line">  <span class="token key atrule">exclude</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> binaries/<span class="token important">**/*.o</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactsexpire_in"><a aria-hidden="true" tabindex="-1" href="#artifactsexpire_in"><span class="icon icon-link"></span></a>artifacts:expire_in</h3><div class="wrap-body">
<p>指定作业 artifacts 在它们过期和被删除之前存储多长时间</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">expire_in</span><span class="token punctuation">:</span> 1 week
</span></code></pre>
<hr>
<ul class="cols-2">
<li>'42'</li>
<li>42 seconds</li>
<li>3 mins 4 sec</li>
<li>2 hrs 20 min</li>
<li>2h20min</li>
<li>6 mos 1 day</li>
<li>47 yrs 6 mos and 4d</li>
<li>3 weeks and 2 days</li>
<li>never</li>
</ul>
<!--rehype:className=cols-2-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactsexpose_as"><a aria-hidden="true" tabindex="-1" href="#artifactsexpose_as"><span class="icon icon-link"></span></a>artifacts:expose_as</h3><div class="wrap-body">
<p>使用 <code>artifacts:expose_as</code> 关键字在合并请求 UI 中公开作业 artifacts</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">test</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"echo 'test' > file.txt"</span><span class="token punctuation">]</span>
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">expose_as</span><span class="token punctuation">:</span> <span class="token string">'artifact 1'</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'file.txt'</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactsname"><a aria-hidden="true" tabindex="-1" href="#artifactsname"><span class="icon icon-link"></span></a>artifacts:name</h3><div class="wrap-body">
<p>定义创建的 <code>artifacts</code> 存档的名称。您可以为每个存档指定一个唯一的名称</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> <span class="token string">"job1-artifacts-file"</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> binaries/
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactspublic"><a aria-hidden="true" tabindex="-1" href="#artifactspublic"><span class="icon icon-link"></span></a>artifacts:public</h3><div class="wrap-body">
<p>确定作业工件是否应该公开可用</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">public</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactsreports"><a aria-hidden="true" tabindex="-1" href="#artifactsreports"><span class="icon icon-link"></span></a>artifacts:reports</h3><div class="wrap-body">
<p>收集作业中包含的模板生成的 <code>artifacts</code></p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">rspec</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> bundle install
</span><span class="code-line">    <span class="token punctuation">-</span> rspec <span class="token punctuation">-</span><span class="token punctuation">-</span>format RspecJunitFormatter <span class="token punctuation">-</span><span class="token punctuation">-</span>out rspec.xml
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">reports</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token key atrule">junit</span><span class="token punctuation">:</span> rspec.xml
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactsuntracked"><a aria-hidden="true" tabindex="-1" href="#artifactsuntracked"><span class="icon icon-link"></span></a>artifacts:untracked</h3><div class="wrap-body">
<p>将所有 Git 未跟踪文件添加为 <code>artifacts</code>（连同在 <code>artifacts:paths</code> 中定义的路径）</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">untracked</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="artifactswhen"><a aria-hidden="true" tabindex="-1" href="#artifactswhen"><span class="icon icon-link"></span></a>artifacts:when</h3><div class="wrap-body">
<p>作业失败或尽管失败时上传 <code>artifacts</code></p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">when</span><span class="token punctuation">:</span> on_failure
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="cache"><a aria-hidden="true" tabindex="-1" href="#cache"><span class="icon icon-link"></span></a>cache</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="cachepaths"><a aria-hidden="true" tabindex="-1" href="#cachepaths"><span class="icon icon-link"></span></a>cache:paths</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">rspec</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "此作业使用缓存"
</span><span class="code-line">  <span class="token key atrule">cache</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">key</span><span class="token punctuation">:</span> binaries<span class="token punctuation">-</span>cache
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> binaries/<span class="token important">*.apk</span>
</span><span class="code-line">      <span class="token punctuation">-</span> .config
</span></code></pre>
<p>关键字来选择要缓存的文件或目录</p>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="cachekey"><a aria-hidden="true" tabindex="-1" href="#cachekey"><span class="icon icon-link"></span></a>cache:key</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>为每个缓存提供唯一的标识键。 使用相同缓存键的所有作业都使用相同的缓存，包括在不同的管道中</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">cache-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "此作业使用缓存"
</span><span class="code-line">  <span class="token key atrule">cache</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">key</span><span class="token punctuation">:</span> binaries<span class="token punctuation">-</span>cache<span class="token punctuation">-</span>$CI_COMMIT_REF_SLUG
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> binaries/
</span></code></pre>
<h4 id="cachekeyfiles"><a aria-hidden="true" tabindex="-1" href="#cachekeyfiles"><span class="icon icon-link"></span></a><code>cache:key:files</code></h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">cache-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "此作业使用缓存"
</span><span class="code-line">  <span class="token key atrule">cache</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">key</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token key atrule">files</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token punctuation">-</span> Gemfile.lock
</span><span class="code-line">        <span class="token punctuation">-</span> package.json
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> vendor/ruby
</span><span class="code-line">      <span class="token punctuation">-</span> node_modules
</span></code></pre>
<p>当一个或两个特定文件更改时，使用 <code>cache:key:files</code> 关键字生成新密钥</p>
<h4 id="cachekeyprefix"><a aria-hidden="true" tabindex="-1" href="#cachekeyprefix"><span class="icon icon-link"></span></a><code>cache:key:prefix</code></h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">rspec</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "此 rspec 作业使用缓存"
</span><span class="code-line">  <span class="token key atrule">cache</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">key</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token key atrule">files</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token punctuation">-</span> Gemfile.lock
</span><span class="code-line">      <span class="token key atrule">prefix</span><span class="token punctuation">:</span> $CI_JOB_NAME
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> vendor/ruby
</span></code></pre>
<p>使用 <code>cache:key:prefix</code> 将前缀与为 <code>cache:key:files</code> 计算的 SHA 结合起来</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="cacheuntracked"><a aria-hidden="true" tabindex="-1" href="#cacheuntracked"><span class="icon icon-link"></span></a>cache:untracked</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">rspec</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">cache</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">untracked</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> binaries/
</span></code></pre>
<p>使用 <code>untracked: true</code> 缓存 Git 存储库中所有未跟踪的文件</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="cachewhen"><a aria-hidden="true" tabindex="-1" href="#cachewhen"><span class="icon icon-link"></span></a>cache:when</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">rspec</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> rspec
</span><span class="code-line">  <span class="token key atrule">cache</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> rspec/
</span><span class="code-line">    <span class="token key atrule">when</span><span class="token punctuation">:</span> <span class="token string">'always'</span>
</span></code></pre>
<p>使用 <code>cache:when</code> 根据作业的状态定义何时保存缓存</p>





















<table><thead><tr><th align="left">:-</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>on_succes(默认)</code></td><td>仅当作业成功时才保存缓存</td></tr><tr><td align="left"><code>on_failure</code></td><td>仅在作业失败时才保存缓存</td></tr><tr><td align="left"><code>always</code></td><td>始终保存缓存</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="cachepolicy"><a aria-hidden="true" tabindex="-1" href="#cachepolicy"><span class="icon icon-link"></span></a>cache:policy</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">prepare-dependencies-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">cache</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">key</span><span class="token punctuation">:</span> gems
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> vendor/bundle
</span><span class="code-line">    <span class="token key atrule">policy</span><span class="token punctuation">:</span> push
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "此作业仅下载依赖项并构建缓存"
</span><span class="code-line">    <span class="token punctuation">-</span> echo "正在下载依赖<span class="token punctuation">...</span>"
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">faster-test-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">cache</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">key</span><span class="token punctuation">:</span> gems
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> vendor/bundle
</span><span class="code-line">    <span class="token key atrule">policy</span><span class="token punctuation">:</span> pull
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "此作业脚本使用缓存，但不更新它"
</span><span class="code-line">    <span class="token punctuation">-</span> echo "运行测试<span class="token punctuation">...</span>"
</span></code></pre>
<p>要更改缓存的上传和下载行为，请使用 <code>cache:policy</code> 关键字</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="environment"><a aria-hidden="true" tabindex="-1" href="#environment"><span class="icon icon-link"></span></a>environment</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="environmentname"><a aria-hidden="true" tabindex="-1" href="#environmentname"><span class="icon icon-link"></span></a>environment:name</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploy to production</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> git push production HEAD<span class="token punctuation">:</span>main
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> production
</span></code></pre>
<p>为环境设置名称</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="environmenturl"><a aria-hidden="true" tabindex="-1" href="#environmenturl"><span class="icon icon-link"></span></a>environment:url</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploy to production</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> git push production HEAD<span class="token punctuation">:</span>main
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> production
</span><span class="code-line">    <span class="token key atrule">url</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//prod.example.com
</span></code></pre>
<p>为环境设置 URL</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="environmenton_stop"><a aria-hidden="true" tabindex="-1" href="#environmenton_stop"><span class="icon icon-link"></span></a>environment:on_stop</h3><div class="wrap-body">
<p>关闭（停止）环境可以通过在环境下定义的 on_stop 关键字来实现。 它声明运行以关闭环境的不同作业</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="environmentaction"><a aria-hidden="true" tabindex="-1" href="#environmentaction"><span class="icon icon-link"></span></a>environment:action</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">stop_review_app</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">GIT_STRATEGY</span><span class="token punctuation">:</span> none
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> make delete<span class="token punctuation">-</span>app
</span><span class="code-line">  <span class="token key atrule">when</span><span class="token punctuation">:</span> manual
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> review/$CI_COMMIT_REF_SLUG
</span><span class="code-line">    <span class="token key atrule">action</span><span class="token punctuation">:</span> stop
</span></code></pre>





























<table><thead><tr><th align="left">:-</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>start</code> <em>默认值</em></td><td>指示作业启动环境。在作业开始后创建的</td></tr><tr><td align="left"><code>prepare</code></td><td>表示作业只是准备环境。它不会触发部署</td></tr><tr><td align="left"><code>stop</code></td><td>指示作业停止部署</td></tr><tr><td align="left"><code>verify</code></td><td>指示作业仅验证环境。它不会触发部署</td></tr><tr><td align="left"><code>access</code></td><td>指示作业仅访问环境。它不会触发部署</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="environmentauto_stop_in"><a aria-hidden="true" tabindex="-1" href="#environmentauto_stop_in"><span class="icon icon-link"></span></a>environment:auto_stop_in</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">review_app</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> deploy<span class="token punctuation">-</span>review<span class="token punctuation">-</span>app
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> review/$CI_COMMIT_REF_SLUG
</span><span class="code-line">    <span class="token key atrule">auto_stop_in</span><span class="token punctuation">:</span> 1 day
</span></code></pre>
<ul class="cols-2">
<li><code>168 hours</code></li>
<li><code>7 days</code></li>
<li><code>one week</code></li>
<li><code>never</code></li>
</ul>
<!--rehype:className=cols-2-->
<p><code>auto_stop_in</code> 关键字指定环境的生命周期。 当环境到期时，GitLab 会自动停止它</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="environmentkubernetes"><a aria-hidden="true" tabindex="-1" href="#environmentkubernetes"><span class="icon icon-link"></span></a>environment:kubernetes</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploy</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> make deploy<span class="token punctuation">-</span>app
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> production
</span><span class="code-line">    <span class="token key atrule">kubernetes</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token key atrule">namespace</span><span class="token punctuation">:</span> production
</span></code></pre>
<p>使用 <code>kubernetes</code> 关键字将部署配置到与您的项目关联的 <code>Kubernetes</code> 集群</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="environmentdeployment_tier"><a aria-hidden="true" tabindex="-1" href="#environmentdeployment_tier"><span class="icon icon-link"></span></a>environment:deployment_tier</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploy</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> customer<span class="token punctuation">-</span>portal
</span><span class="code-line">    <span class="token key atrule">deployment_tier</span><span class="token punctuation">:</span> production
</span></code></pre>
<ul class="cols-2">
<li><code>production</code></li>
<li><code>staging</code></li>
<li><code>testing</code></li>
<li><code>development</code></li>
<li><code>other</code></li>
</ul>
<!--rehype:className=cols-2-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="动态-environment"><a aria-hidden="true" tabindex="-1" href="#动态-environment"><span class="icon icon-link"></span></a>动态 environment</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploy as review app</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> make deploy
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> review/$CI_COMMIT_REF_SLUG
</span><span class="code-line">    <span class="token key atrule">url</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//$CI_ENVIRONMENT_SLUG.example.com/
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="image"><a aria-hidden="true" tabindex="-1" href="#image"><span class="icon icon-link"></span></a>image</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="imagename"><a aria-hidden="true" tabindex="-1" href="#imagename"><span class="icon icon-link"></span></a>image:name</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">image</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">name</span><span class="token punctuation">:</span> <span class="token string">"registry.example.com/my/image:latest"</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>作业运行所在的 Docker 镜像的名称。类似于它自己使用的镜像</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="imageentrypoint"><a aria-hidden="true" tabindex="-1" href="#imageentrypoint"><span class="icon icon-link"></span></a>image:entrypoint</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">image</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">name</span><span class="token punctuation">:</span> super/sql<span class="token punctuation">:</span>experimental
</span><span class="code-line">  <span class="token key atrule">entrypoint</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">""</span><span class="token punctuation">]</span>
</span></code></pre>
<p>作为容器入口点执行的命令或脚本</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="imagepull_policy"><a aria-hidden="true" tabindex="-1" href="#imagepull_policy"><span class="icon icon-link"></span></a>image:pull_policy</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "单一拉动政策"
</span><span class="code-line">  <span class="token key atrule">image</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> ruby<span class="token punctuation">:</span><span class="token number">3.0</span>
</span><span class="code-line">    <span class="token key atrule">pull_policy</span><span class="token punctuation">:</span> if<span class="token punctuation">-</span>not<span class="token punctuation">-</span>present
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "多重拉动政策"
</span><span class="code-line">  <span class="token key atrule">image</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> ruby<span class="token punctuation">:</span><span class="token number">3.0</span>
</span><span class="code-line">    <span class="token key atrule">pull_policy</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>always<span class="token punctuation">,</span> if<span class="token punctuation">-</span>not<span class="token punctuation">-</span>present<span class="token punctuation">]</span>
</span></code></pre>
<p>运行器用于获取 Docker 映像的拉取策略</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="needs"><a aria-hidden="true" tabindex="-1" href="#needs"><span class="icon icon-link"></span></a>needs</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="needsartifacts"><a aria-hidden="true" tabindex="-1" href="#needsartifacts"><span class="icon icon-link"></span></a>needs:artifacts</h3><div class="wrap-body">
<p>当作业使用需求时，它不再默认下载先前阶段的所有工件，因为有需求的作业可以在早期阶段完成之前开始</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">test-job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">needs</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">job</span><span class="token punctuation">:</span> build_job1
</span><span class="code-line">      <span class="token key atrule">artifacts</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test-job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">needs</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">job</span><span class="token punctuation">:</span> build_job2
</span><span class="code-line">      <span class="token key atrule">artifacts</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test-job3</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">needs</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">job</span><span class="token punctuation">:</span> build_job1
</span><span class="code-line">      <span class="token key atrule">artifacts</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">job</span><span class="token punctuation">:</span> build_job2
</span><span class="code-line">    <span class="token punctuation">-</span> build_job3
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="needsproject"><a aria-hidden="true" tabindex="-1" href="#needsproject"><span class="icon icon-link"></span></a>needs:project</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<p>使用 <code>needs:project</code> 从其他管道中的最多五个作业下载工件。工件是从指定参考的最新成功管道下载的。要指定多个作业，请将每个作业作为单独的数组项添加到 needs 关键字下</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">build_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> ls <span class="token punctuation">-</span>lhR
</span><span class="code-line">  <span class="token key atrule">needs</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">project</span><span class="token punctuation">:</span> namespace/group/project<span class="token punctuation">-</span>name
</span><span class="code-line">      <span class="token key atrule">job</span><span class="token punctuation">:</span> build<span class="token punctuation">-</span><span class="token number">1</span>
</span><span class="code-line">      <span class="token key atrule">ref</span><span class="token punctuation">:</span> main
</span><span class="code-line">      <span class="token key atrule">artifacts</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">project</span><span class="token punctuation">:</span> namespace/group/project<span class="token punctuation">-</span>name<span class="token punctuation">-</span><span class="token number">2</span>
</span><span class="code-line">      <span class="token key atrule">job</span><span class="token punctuation">:</span> build<span class="token punctuation">-</span><span class="token number">2</span>
</span><span class="code-line">      <span class="token key atrule">ref</span><span class="token punctuation">:</span> main
</span><span class="code-line">      <span class="token key atrule">artifacts</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="needspipelinejob"><a aria-hidden="true" tabindex="-1" href="#needspipelinejob"><span class="icon icon-link"></span></a>needs:pipeline:job</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">create-artifact</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "sample artifact" <span class="token punctuation">></span> artifact.txt
</span><span class="code-line">  <span class="token key atrule">artifacts</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">paths</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>artifact.txt<span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">child-pipeline</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">trigger</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">include</span><span class="token punctuation">:</span> child.yml
</span><span class="code-line">    <span class="token key atrule">strategy</span><span class="token punctuation">:</span> depend
</span><span class="code-line">  <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">PARENT_PIPELINE_ID</span><span class="token punctuation">:</span> $CI_PIPELINE_ID
</span></code></pre>
<p>子管道可以从其父管道中的作业或同一父子管道层次结构中的另一个子管道下载工件</p>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="needsoptional"><a aria-hidden="true" tabindex="-1" href="#needsoptional"><span class="icon icon-link"></span></a>needs:optional</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->
<p>要需要管道中有时不存在的作业，请将 <code>optional: true</code> 添加到需求配置中。如果未定义，则可选：默认为 <code>false</code></p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">build-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> build
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test-job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">test-job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">deploy-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">needs</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">job</span><span class="token punctuation">:</span> test<span class="token punctuation">-</span>job2
</span><span class="code-line">      <span class="token key atrule">optional</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">job</span><span class="token punctuation">:</span> test<span class="token punctuation">-</span>job1
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> production
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">review-job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> deploy
</span><span class="code-line">  <span class="token key atrule">needs</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">job</span><span class="token punctuation">:</span> test<span class="token punctuation">-</span>job2
</span><span class="code-line">      <span class="token key atrule">optional</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">  <span class="token key atrule">environment</span><span class="token punctuation">:</span> review
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="needspipeline"><a aria-hidden="true" tabindex="-1" href="#needspipeline"><span class="icon icon-link"></span></a>needs:pipeline</h3><div class="wrap-body">
<p>您可以使用 <code>needs:pipeline</code> 关键字将管道状态从上游管道镜像到桥接作业</p>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">upstream_bridge</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> test
</span><span class="code-line">  <span class="token key atrule">needs</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">pipeline</span><span class="token punctuation">:</span> other/project
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="only--except"><a aria-hidden="true" tabindex="-1" href="#only--except"><span class="icon icon-link"></span></a>only / except</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="onlyrefs--exceptrefs"><a aria-hidden="true" tabindex="-1" href="#onlyrefs--exceptrefs"><span class="icon icon-link"></span></a>only:refs / except:refs</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo
</span><span class="code-line">  <span class="token key atrule">only</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> main
</span><span class="code-line">    <span class="token punctuation">-</span> /^issue<span class="token punctuation">-</span>.<span class="token important">*$/</span>
</span><span class="code-line">    <span class="token punctuation">-</span> merge_requests
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">job2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo
</span><span class="code-line">  <span class="token key atrule">except</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> main
</span><span class="code-line">    <span class="token punctuation">-</span> /^stable<span class="token punctuation">-</span>branch.<span class="token important">*$/</span>
</span><span class="code-line">    <span class="token punctuation">-</span> schedules
</span></code></pre>
<p>使用 <code>only:refs</code> 和 <code>except:refs</code> 关键字来控制何时根据分支名称或管道类型将作业添加到管道</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="onlyvariables--exceptvariables"><a aria-hidden="true" tabindex="-1" href="#onlyvariables--exceptvariables"><span class="icon icon-link"></span></a>only:variables / except:variables</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploy</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> cap staging deploy
</span><span class="code-line">  <span class="token key atrule">only</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> $RELEASE == "staging"
</span><span class="code-line">      <span class="token punctuation">-</span> $STAGING
</span></code></pre>
<p>根据 CI/CD 变量的状态，使用 only:variables 或 except:variables 关键字来控制何时将作业添加到管道</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="onlychanges--exceptchanges"><a aria-hidden="true" tabindex="-1" href="#onlychanges--exceptchanges"><span class="icon icon-link"></span></a>only:changes / except:changes</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">docker build</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> docker build <span class="token punctuation">-</span>t my<span class="token punctuation">-</span>image<span class="token punctuation">:</span>$CI_COMMIT_REF_SLUG .
</span><span class="code-line">  <span class="token key atrule">only</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">refs</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> branches
</span><span class="code-line">    <span class="token key atrule">changes</span><span class="token punctuation">:</span>
</span><span class="code-line">      <span class="token punctuation">-</span> Dockerfile
</span><span class="code-line">      <span class="token punctuation">-</span> docker/scripts/*
</span><span class="code-line">      <span class="token punctuation">-</span> dockerfiles/<span class="token important">**/*</span>
</span><span class="code-line">      <span class="token punctuation">-</span> more_scripts/<span class="token important">*.</span><span class="token punctuation">{</span>rb<span class="token punctuation">,</span>py<span class="token punctuation">,</span>sh<span class="token punctuation">}</span>
</span><span class="code-line">      <span class="token punctuation">-</span> <span class="token string">"**/*.json"</span>
</span></code></pre>
<p>当 <code>Git</code> 推送事件修改文件时，使用 <code>changes</code> 关键字仅运行作业，或使用 <code>except</code> 跳过作业</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="onlykubernetes--exceptkubernetes"><a aria-hidden="true" tabindex="-1" href="#onlykubernetes--exceptkubernetes"><span class="icon icon-link"></span></a>only:kubernetes / except:kubernetes</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">deploy</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">only</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">kubernetes</span><span class="token punctuation">:</span> active
</span></code></pre>
<p>使用 <code>only:kubernetes</code> 或 <code>except:kubernetes</code> 来控制当 <code>Kubernetes</code> 服务在项目中处于活动状态时是否将作业添加到管道中</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="release"><a aria-hidden="true" tabindex="-1" href="#release"><span class="icon icon-link"></span></a>release</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="release-参考"><a aria-hidden="true" tabindex="-1" href="#release-参考"><span class="icon icon-link"></span></a>release 参考</h3><div class="wrap-body">





































<table><thead><tr><th align="left">:-</th><th>--</th></tr></thead><tbody><tr><td align="left"><a href="#releasetag_name">release:tag_name</a></td><td>发布的 Git 标签</td></tr><tr><td align="left"><a href="#releasetag_message">release:tag_message</a></td><td>指定的消息进行注释</td></tr><tr><td align="left"><a href="#releasename">release:name</a></td><td>发布名称</td></tr><tr><td align="left"><a href="#releasedescription">release:description</a></td><td>版本的详细描述</td></tr><tr><td align="left"><a href="#releaseref">release:ref</a></td><td>发布的 ref</td></tr><tr><td align="left"><a href="#releasemilestones">release:milestones</a></td><td>每个里程碑的标题</td></tr><tr><td align="left"><a href="#releasereleased_at">release:released_at</a></td><td>发布准备就绪的日期和时间</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="releasetag_name"><a aria-hidden="true" tabindex="-1" href="#releasetag_name"><span class="icon icon-link"></span></a>release:tag_name</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "为新标签运行发布作业"
</span><span class="code-line">  <span class="token key atrule">release</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">tag_name</span><span class="token punctuation">:</span> $CI_COMMIT_TAG
</span><span class="code-line">    <span class="token key atrule">description</span><span class="token punctuation">:</span> <span class="token string">'Release description'</span>
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_TAG
</span></code></pre>
<p>必需的。发布的 Git 标签</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="releasetag_message"><a aria-hidden="true" tabindex="-1" href="#releasetag_message"><span class="icon icon-link"></span></a>release:tag_message</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">release_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> release
</span><span class="code-line">  <span class="token key atrule">release</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">tag_name</span><span class="token punctuation">:</span> $CI_COMMIT_TAG
</span><span class="code-line">    <span class="token key atrule">description</span><span class="token punctuation">:</span> <span class="token string">'Release description'</span>
</span><span class="code-line">    <span class="token key atrule">tag_message</span><span class="token punctuation">:</span> <span class="token string">'Annotated tag message'</span>
</span></code></pre>
<p>如果标签不存在，则新创建的标签将使用 <code>tag_message</code> 指定的消息进行注释</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="releasename"><a aria-hidden="true" tabindex="-1" href="#releasename"><span class="icon icon-link"></span></a>release:name</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">release_job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">stage</span><span class="token punctuation">:</span> release
</span><span class="code-line">  <span class="token key atrule">release</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">name</span><span class="token punctuation">:</span> <span class="token string">'Release $CI_COMMIT_TAG'</span>
</span></code></pre>
<p>发布名称。如果省略，它会填充 <code>release:tag_name</code> 的值</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="releasedescription"><a aria-hidden="true" tabindex="-1" href="#releasedescription"><span class="icon icon-link"></span></a>release:description</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">release</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">tag_name</span><span class="token punctuation">:</span> $<span class="token punctuation">{</span>MAJOR<span class="token punctuation">}</span>_$<span class="token punctuation">{</span>MINOR<span class="token punctuation">}</span>_$<span class="token punctuation">{</span>REVISION<span class="token punctuation">}</span>
</span><span class="code-line">    <span class="token key atrule">description</span><span class="token punctuation">:</span> <span class="token string">'./path/to/CHANGELOG.md'</span>
</span></code></pre>
<p>版本的详细描述</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="releaseref"><a aria-hidden="true" tabindex="-1" href="#releaseref"><span class="icon icon-link"></span></a>release:ref</h3><div class="wrap-body">
<p>发布的 ref，如果发布：<code>tag_name</code> 尚不存在</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="releasemilestones"><a aria-hidden="true" tabindex="-1" href="#releasemilestones"><span class="icon icon-link"></span></a>release:milestones</h3><div class="wrap-body">
<p>与版本关联的每个里程碑的标题</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="releasereleased_at"><a aria-hidden="true" tabindex="-1" href="#releasereleased_at"><span class="icon icon-link"></span></a>release:released_at</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">released_at</span><span class="token punctuation">:</span> <span class="token string">'2021-03-15T08:00:00Z'</span>
</span></code></pre>
<p>发布准备就绪的日期和时间</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="releaseassetslinks"><a aria-hidden="true" tabindex="-1" href="#releaseassetslinks"><span class="icon icon-link"></span></a>release:assets:links</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">assets</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">links</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> <span class="token string">'asset1'</span>
</span><span class="code-line">      <span class="token key atrule">url</span><span class="token punctuation">:</span> <span class="token string">'https://example.com/assets/1'</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> <span class="token string">'asset2'</span>
</span><span class="code-line">      <span class="token key atrule">url</span><span class="token punctuation">:</span> <span class="token string">'https://example.com/assets/2'</span>
</span><span class="code-line">      <span class="token key atrule">filepath</span><span class="token punctuation">:</span> <span class="token string">'/pretty/url/1'</span> <span class="token comment"># 可选的</span>
</span><span class="code-line">      <span class="token key atrule">link_type</span><span class="token punctuation">:</span> <span class="token string">'other'</span> <span class="token comment"># 可选的</span>
</span></code></pre>
<p>使用 <code>release:assets:links</code> 在发布中包含资产链接</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="rules"><a aria-hidden="true" tabindex="-1" href="#rules"><span class="icon icon-link"></span></a>rules</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="rulesif"><a aria-hidden="true" tabindex="-1" href="#rulesif"><span class="icon icon-link"></span></a>rules:if</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "Hello<span class="token punctuation">,</span> Rules<span class="token tag">!</span>"
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature/ <span class="token important">&#x26;&#x26;</span> $CI_MERGE_REQUEST_TARGET_BRANCH_NAME <span class="token tag">!=</span> $CI_DEFAULT_BRANCH
</span><span class="code-line">      <span class="token key atrule">when</span><span class="token punctuation">:</span> never
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature/
</span><span class="code-line">      <span class="token key atrule">when</span><span class="token punctuation">:</span> manual
</span><span class="code-line">      <span class="token key atrule">allow_failure</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
</span></code></pre>
<p>使用 <code>rules:if</code> 子句指定何时将作业添加到管道</p>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="ruleschanges"><a aria-hidden="true" tabindex="-1" href="#ruleschanges"><span class="icon icon-link"></span></a>rules:changes</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">docker build</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> docker build <span class="token punctuation">-</span>t my<span class="token punctuation">-</span>image<span class="token punctuation">:</span>$CI_COMMIT_REF_SLUG .
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_PIPELINE_SOURCE == "merge_request_event"
</span><span class="code-line">      <span class="token key atrule">changes</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token punctuation">-</span> Dockerfile
</span><span class="code-line">      <span class="token key atrule">when</span><span class="token punctuation">:</span> manual
</span><span class="code-line">      <span class="token key atrule">allow_failure</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span></code></pre>
<p>使用 <code>rules:changes</code> 通过检查对特定文件的更改来指定何时将作业添加到管道</p>
<h4 id="ruleschangespaths"><a aria-hidden="true" tabindex="-1" href="#ruleschangespaths"><span class="icon icon-link"></span></a>rules:changes:paths</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">docker-build-1</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> docker build <span class="token punctuation">-</span>t my<span class="token punctuation">-</span>image<span class="token punctuation">:</span>$CI_COMMIT_REF_SLUG .
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_PIPELINE_SOURCE == "merge_request_event"
</span><span class="code-line">      <span class="token key atrule">changes</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token punctuation">-</span> Dockerfile
</span><span class="code-line">
</span><span class="code-line"><span class="token key atrule">docker-build-2</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> docker build <span class="token punctuation">-</span>t my<span class="token punctuation">-</span>image<span class="token punctuation">:</span>$CI_COMMIT_REF_SLUG .
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_PIPELINE_SOURCE == "merge_request_event"
</span><span class="code-line">      <span class="token key atrule">changes</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">          <span class="token punctuation">-</span> Dockerfile
</span></code></pre>
<p>使用 <code>rules:changes</code> 指定仅在更改特定文件时将作业添加到管道，并使用 <code>rules:changes:paths</code> 指定文件</p>
<h4 id="ruleschangescompare_to"><a aria-hidden="true" tabindex="-1" href="#ruleschangescompare_to"><span class="icon icon-link"></span></a>rules:changes:compare_to</h4>
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">docker build</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> docker build <span class="token punctuation">-</span>t my<span class="token punctuation">-</span>image<span class="token punctuation">:</span>$CI_COMMIT_REF_SLUG .
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_PIPELINE_SOURCE == "merge_request_event"
</span><span class="code-line">      <span class="token key atrule">changes</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token key atrule">paths</span><span class="token punctuation">:</span>
</span><span class="code-line">          <span class="token punctuation">-</span> Dockerfile
</span><span class="code-line">        <span class="token key atrule">compare_to</span><span class="token punctuation">:</span> <span class="token string">'refs/heads/branch1'</span>
</span></code></pre>
<p>使用 <code>rules:changes:compare_to</code> 指定要比较哪个 <code>ref</code> 来比较 <code>rules:changes:paths</code> 下列出的文件的更改</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="rulesexists"><a aria-hidden="true" tabindex="-1" href="#rulesexists"><span class="icon icon-link"></span></a>rules:exists</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> docker build <span class="token punctuation">-</span>t my<span class="token punctuation">-</span>image<span class="token punctuation">:</span>$CI_COMMIT_REF_SLUG .
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">exists</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token punctuation">-</span> Dockerfile
</span></code></pre>
<p>当存储库中存在某些文件时，使用 <code>exists</code> 运行作业</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="rulesallow_failure"><a aria-hidden="true" tabindex="-1" href="#rulesallow_failure"><span class="icon icon-link"></span></a>rules:allow_failure</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span> echo "Hello<span class="token punctuation">,</span> Rules<span class="token tag">!</span>"
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH
</span><span class="code-line">      <span class="token key atrule">when</span><span class="token punctuation">:</span> manual
</span><span class="code-line">      <span class="token key atrule">allow_failure</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</span></code></pre>
<p>在规则中使用 <code>allow_failure: true</code> 允许作业失败而不停止管道</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="rulesvariables"><a aria-hidden="true" tabindex="-1" href="#rulesvariables"><span class="icon icon-link"></span></a>rules:variables</h3><div class="wrap-body">
<pre class="language-yml"><code class="language-yml code-highlight"><span class="code-line"><span class="token key atrule">job</span><span class="token punctuation">:</span>
</span><span class="code-line">  <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token key atrule">DEPLOY_VARIABLE</span><span class="token punctuation">:</span> <span class="token string">"default-deploy"</span>
</span><span class="code-line">  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
</span><span class="code-line">      <span class="token key atrule">variables</span><span class="token punctuation">:</span>                              <span class="token comment"># 覆盖定义的 DEPLOY_VARIABLE</span>
</span><span class="code-line">        <span class="token key atrule">DEPLOY_VARIABLE</span><span class="token punctuation">:</span> <span class="token string">"deploy-production"</span>  <span class="token comment"># 在工作层面</span>
</span><span class="code-line">    <span class="token punctuation">-</span> <span class="token key atrule">if</span><span class="token punctuation">:</span> $CI_COMMIT_REF_NAME =~ /feature/
</span><span class="code-line">      <span class="token key atrule">variables</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token key atrule">IS_A_FEATURE</span><span class="token punctuation">:</span> <span class="token string">"true"</span>                  <span class="token comment"># 定义一个新变量</span>
</span><span class="code-line">  <span class="token key atrule">script</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token punctuation">-</span> echo "以 $DEPLOY_VARIABLE 作为参数运行脚本"
</span><span class="code-line">    <span class="token punctuation">-</span> echo "如果 $IS_A_FEATURE 存在则运行另一个脚本"
</span></code></pre>
<p>在规则中使用变量来为特定条件定义变量</p>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://docs.gitlab.com/ee/ci/yaml/">.gitlab-ci.yml 关键字参考</a></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>.</footer></footer><script src="../data.js?v=1.5.5" defer></script><script src="../js/fuse.min.js?v=1.5.5" defer></script><script src="../js/main.js?v=1.5.5" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
